package org.dtrd.modules.achievement.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.CheckUtil;
import org.dtrd.common.util.DateUtils;
import org.dtrd.modules.achievement.entity.bean.Achievement;
import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninAchievement;
import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninPlan;
import org.dtrd.modules.achievement.mapper.DtrdEntRdSigninAchievementMapper;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninAchievementService;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 萧
 * @date 2021-09-21 22:50:23
 **/
@Service
@DS("multi-datasource1")
@Slf4j
public class DtrdEntRdSigninAchievementServiceImpl extends ServiceImpl<DtrdEntRdSigninAchievementMapper, DtrdEntRdSigninAchievement> implements IDtrdEntRdSigninAchievementService {

    @Autowired
    private IDtrdEntRdSigninPlanService signinPlanService;

    @Override
    public void saveDailyScore(Integer patientId, String recordDate, int score) {
        DtrdEntRdSigninAchievement entity = getDailyScore(recordDate, patientId);
        log.info("DtrdEntRdSigninAchievementServiceImpl.saveDailyScore.[patientId, recordDate, score] entity = " + entity);
        if (entity == null) {
            entity = new DtrdEntRdSigninAchievement().init();
        }
        entity.setAchievementScore(score);
        entity.setPatientId(patientId);
        Date date;
        if (recordDate != null) {
            try {
                date = DateUtils.parseDate(recordDate, DateUtils.date_sdf.get().toPattern());
            } catch (ParseException e) {
                date = new Date();
                e.printStackTrace();
            }
        } else {
            date = new Date();
        }
        entity.setRecordDate(date);
        log.info("DtrdEntRdSigninAchievementServiceImpl.saveDailyScore.[patientId, recordDate, score] entity = " + entity);
        saveOrUpdate(entity);
    }

    @Override
    public Achievement getDailyScore(Integer patientId, String recordDate) {
        if (patientId == null) {
            return null;
        }
        if (recordDate == null) {
            return null;
        }
        DtrdEntRdSigninAchievement dailyScore = getDailyScore(recordDate, patientId);
        return Optional.ofNullable(dailyScore).map(score -> new Achievement().parseFromPO(score)).orElse(null);
    }

    private DtrdEntRdSigninAchievement getDailyScore(String recordDate, Integer patientId) {
        List<DtrdEntRdSigninAchievement> list = baseMapper.selectScoreByPatientIdDate(patientId, recordDate);
        log.info("DtrdEntRdSigninAchievementServiceImpl.getDailyScore.[recordDate, patientId] list.size() = " + list.size());
        if (CheckUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Achievement> getScoreList(Integer patientId, String recordDate) {
        List<DtrdEntRdSigninAchievement> achievementList = baseMapper.selectScoreByPatientIdDate(patientId, recordDate);
        return Optional.ofNullable(achievementList).map(list -> list.stream().map(achievement -> new Achievement().parseFromPO(achievement)).collect(Collectors.toList())).orElse(null);
    }

    @Override
    public List<Achievement> getAchievementList(Integer patientId, String startDate, String endDate) {
        List<DtrdEntRdSigninAchievement> achievementList = baseMapper.selectScoreByPatientIdDatePeriod(patientId, startDate, endDate);
        return Optional.ofNullable(achievementList).map(list -> list.stream().map(achievement -> new Achievement().parseFromPO(achievement)).collect(Collectors.toList())).orElse(null);
    }

    @Override
    public void summaryCheckinDays() {
        List<DtrdEntRdSigninPlan> planList = signinPlanService.getAllEffectivePlans();
        if (CheckUtil.isNotEmpty(planList)){
            planList.stream().map(DtrdEntRdSigninPlan::getPatientId).collect(Collectors.toList());
            String yestoday = DateUtils.dateAdd(-1);
            // TODO 打卡天数调试
        }
    }


}
